#!/usr/bin/env python
# coding=utf-8

from pymongo import MongoClient
import tushare as ts
import numpy as np
import pandas as pd
import json
import datetime
import os
import sys

# 每天的股票数据的更新的脚本.
# 首先获取数据库中最后的日期, 然后一直更新到今天
# 注意, 这里需要把所有股票数据获取到内存中再统一插入, 不然中间脚本除了什么问题可能处理起来比较麻烦

def get_db():
    client = MongoClient("localhost", 27017)
    db = client.stock
    return db

def get_yestoday():
    yestoday = str(datetime.date.today() - datetime.timedelta(days=1))
    print("yestoday = ", yestoday)
    return yestoday

def get_lastday(db):
    data = db.stocks.find({"code":"hs300"}).sort([("date", -1)]).limit(1)
    data = list(data)
    lastday = data[0]['date']
    print("lastday = ", lastday)
    return lastday

def add_oneday(now):
    now = pd.Timestamp(now)
    now = now + datetime.timedelta(days=1)
    return now.strftime('%Y-%m-%d')

if __name__ == "__main__":
    db = get_db()
    print(db)

    yestoday = get_yestoday()
    lastday = get_lastday(db)
    if pd.Timestamp(yestoday) <= pd.Timestamp(lastday):
        print("nothing to do") 
        sys.exit()

    from_day = add_oneday(lastday)
    print("from_day = ", from_day)

    # sys.exit()

    stocks_today = []
    
    with open("stock_list.txt") as f:
        for line in f:
            line = line.strip()
            if line == '':
                continue
            print(line)
            origin_data = ts.get_k_data(line,  ktype='D', start=from_day)
            if origin_data.empty:
                print(line + ": data is empty")
                continue
            t = json.loads(origin_data.to_json(orient='records', date_format='iso'))
            stocks_today.extend(t)
        
        db.stocks.insert(stocks_today)